package com.osa.birthdaypie;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

public class FlameProvider {
	public static final int FLAME_FRAME_COUNT = 6;

	private Bitmap[][] flameFrames;
	private int rotationCount;

	public FlameProvider(Resources resources, int rotationCount,
			int degreesPerStep) {
		this.rotationCount = rotationCount;
		flameFrames = new Bitmap[rotationCount * 2 + 1][FLAME_FRAME_COUNT];
		final int scale = 2;
		for (int i = 0; i < FLAME_FRAME_COUNT; i++) {
			Bitmap flameSource = BitmapFactory.decodeResource(
					resources, resources.getIdentifier("flame0" + i,
							"drawable", "com.osa.birthdaypie"));
			flameFrames[rotationCount][i] = Bitmap.createScaledBitmap(flameSource, flameSource.getWidth() / scale, flameSource.getHeight() / scale, true);
			//flameFrames[rotationCount][i].setPixel(0, 0, 0xFFFFFFFF);
			for (int j = 1; j <= rotationCount; j++) {
				flameFrames[rotationCount + j][i] = rotate(
						flameSource, j * degreesPerStep, scale);
				flameFrames[rotationCount - j][i] = rotate(
						flameSource, -j * degreesPerStep, scale);
			}
		}
	}

	public int nextFrame(int frame){
		return ++frame % FLAME_FRAME_COUNT;
	}

	public Bitmap getFlame(int frame, int rotation) {
		rotation = trimRotation(rotation);
		Bitmap[] frames = flameFrames[rotation + rotationCount];
		try{
			return frames[frame];
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("frame:" + frame);
			System.out.println("frames length:" + frames.length);
			throw e;
		}
	}
	
	public int getFlameMiddle(int frame, int rotation){
		rotation = trimRotation(rotation);
		int rotation0middle = flameFrames[rotationCount][0].getWidth() / 2;
		int res;
		if(rotation >= 0){
			res = rotation0middle;
		}
		else{
			res = flameFrames[rotation + rotationCount][frame].getWidth() - rotation0middle;
//			System.out.println("width:" + flameFrames[rotation + rotationCount][frame].getWidth());
		}
//		System.out.println("middle: " + rotation0middle + ", res: " + res);
		return res;
	}
	
	private int trimRotation(int rotation){
		if (rotation > rotationCount)
			rotation = rotationCount;
		if (rotation < -rotationCount)
			rotation = -rotationCount;
		return rotation;
	}

	private Bitmap rotate(Bitmap source, int degrees, int scale) {
		Matrix matrix = new Matrix();
		int width = source.getWidth();
		int height = source.getHeight();
		matrix.preTranslate(-(width / 2), -height);
		matrix.postRotate(degrees);
		if(degrees >= 0)
			matrix.postTranslate(width / 2, height);
		else
			matrix.postTranslate(width / 2 + width, height);
		matrix.postScale(1.0f / scale, 1.0f / scale);
		Bitmap res = Bitmap.createBitmap(2 * width / scale, height / scale, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(res);
		canvas.drawBitmap(source, matrix, new Paint());
		return res;
	}
}
